package com.eiokey.baselibrary.annotation;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


/**
 * description  处理注解
 * use
 * param
 * return
 * @author JetQiao
 * @date 2021/7/21  5:55 下午
 */

public class AnnotationHandler
{
    /**
     * description  获取viewModel
     * use
     * param
     * return
     * @author JetQiao
     * @date   2021/7/21  5:55 下午
     */

    public static Class getViewModelClass(Object obj)
    {
        // 避免反射
        ViewModel viewModel = obj.getClass().getAnnotation(ViewModel.class);
        if (viewModel != null)
        {
            return viewModel.value();
        }
        else
        {
            Type type = obj.getClass().getGenericSuperclass();
            ParameterizedType paramtype = null;
            try
            {
                paramtype = (ParameterizedType) type;
            } catch (ClassCastException e)
            {
                return null;
            }
            assert paramtype != null;
            Type[] typeArguments = paramtype.getActualTypeArguments();
            if (typeArguments.length >= 1)
            {
                return (Class) typeArguments[0];
            }
        }

        return null;
    }

}
